package impl

import (
	. "dashenwo.com/dashenwo/micro_srv/search_srv/configs"
	"github.com/micro/go-micro/v2/util/log"
	"github.com/olivere/elastic/v7"
)

type searchRepository struct{}

func NewSearchRepository() *searchRepository {
	return &searchRepository{}
}

func (s *searchRepository) Query(index string, query elastic.Query, OrderBy string, Page int, Size int) (*elastic.SearchResult, error) {
	// 代码高亮
	highlight := elastic.NewHighlight()
	highlight.PreTags("<mark>")
	highlight.PostTags("</mark>")
	// 计算分页
	min := (Page - 1) * Size
	log.Log(min, Size, index, OrderBy)

	searchResult, err := EsClient.Search().
		Index(index).         // search in index "twitter"
		Sort(OrderBy, false). // sort by "user" field, ascending
		Highlight(highlight).
		From(int(min)).Size(int(Size)). // take documents 0-9
		Pretty(true).                   // pretty print request and response JSON
		Do(Ctx)                         // execute
	return searchResult, err
}
